﻿using Coldairarrow.Entity;
using Coldairarrow.Entity.Frontstage.Valickets;
using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.TurnPDF;
using Coldairarrow.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Coldairarrow.Business
{
    public interface IOrder_MasterBusiness
    {
        Task<PageResult<Order_Master>> GetDataListAsync(PageInput<ConditionDTO> input);

        Task<Order_Master> GetTheDataAsync(string id);

        Task AddDataAsync(Order_Master data);

        Task UpdateDataAsync(Order_Master data);

        Task ChangeAddressInfo(ChangeAddressInfoDTO input);

        Task ChangeOrderInvoiceStateAsnyc(IdInputDTO input);
        /// <summary>
        /// 统计
        /// </summary>
        /// <returns></returns>
        Task<List<Statistics>> GetStatistics();
        Task SendOrderAsync(ToSendOrderDTO input);
        /// <summary>
        /// 更新订单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        Task UpdateDataTwoAsync(Order_Master data);

        Task DeleteDataAsync(List<string> ids);

        Task DelDataAsync(List<string> ids);

        #region 前台
        /// <summary>
        /// 统计
        /// </summary>
        /// <returns></returns>
        orderStatistics getStatistics();

        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        Task<List<GoodsDtom>> GetProduct();

        /// <summary>
        /// 生成订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<OrderNum> AddOrderAsync(OrderDto dto);

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="canncl"></param>
        /// <returns></returns>
        Task CancelOrderAsync(CannclOrder canncl);

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        void CancelOrder(string Id);

        /// <summary>
        /// 获取订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<PageResult<OrderProducts>> GetOrderData(PageInput<OrderProducts> input);

        /// <summary>
        /// 获取订单(个人)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<OrderProducts> GetTheOrderData(OrderProducts input);

        #endregion 前台

        /// <summary>
        /// 生成订单——JWF
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<OrderNum> SetToOrderAsync(SetOrderDto dto);

        /// <summary>
        /// 生成订单(事务)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<OrderNum> AddOrderTwoAsync(OrderDto dto);

        /// <summary>
        /// 获取订单
        /// </summary>
        /// <param name="order_"></param>
        /// <returns></returns>
        Task<PageResult<Order_Dto>> GetOrderDataTwo(PageInput<Order_DtoTwo> order_);

        /// <summary>
        /// 生成积分商品订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<OrderNum> PointGoodOrderAsync(PointOrderDto dto);

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        Task<Order_Dto> GetOrderTheDataTwo(string Id);
        Order_Dto GetOrderTheDataTree(InputDTO input);

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <returns></returns>
        Task<PageResult<Order_Decimal>> GetDataOver(PageInput<Order_Decimal> input);

        /// <summary>
        /// 获取前台评论统计数据
        /// </summary>
        /// <returns></returns>
        Task<object> GetOverSum();

        Task<string> GetOrderExploreFile(Order_DtoTwo input);
    }

    public class OrderExplorDTO
    {
        /// <summary>
        /// 用户昵称
        /// </summary>
        public string UserNickName { get; set; } = "";

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserRealName { get; set; } = "";

        /// <summary>
        /// 用户性别
        /// </summary>
        public string Sex { get; set; } = "";

        /// <summary>
        /// 用户电话
        /// </summary>
        public string Phone { get; set; } = "";

        /// <summary>
        /// 收货人姓名
        /// </summary>
        public string ReciverName { get; set; } = "";


        /// <summary>
        /// 收货人电话
        /// </summary>
        public string ReciverPhone { get; set; } = "";

        /// <summary>
        /// 收货人地址
        /// </summary>
        public string ReciverAddress { get; set; } = "";

        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderCode { get; set; } = "";

        /// <summary>
        /// 支付方式
        /// </summary>
        public string PayType { get; set; } = "";

        /// <summary>
        /// 订单状态
        /// </summary>
        public string OrderState { get; set; } = "";


        /// <summary>
        /// 快递方式
        /// </summary>
        public string SendType { get; set; } = "快递免邮";

        /// <summary>
        /// 订单备注
        /// </summary>
        public string OrderNote { get; set; } = "";

        /// <summary>
        /// 订单总价
        /// </summary>
        public string YSPrice { get; set; } = "";

        /// <summary>
        /// 优惠金额
        /// </summary>
        public string DeductPrice { get; set; } = "";

        /// <summary>
        /// 实收金额
        /// </summary>
        public string PayPrice { get; set; } = "";


        /// <summary>
        /// 物流单号
        /// </summary>
        public string OddNum { get; set; } = "";

        /// <summary>
        ///物流公司
        /// </summary>
        public string OddCompany { get; set; } = "";

        /// <summary>
        /// 退款原因
        /// </summary>
        public string RefundReason { get; set; } = "";

        /// <summary>
        /// 退款状态
        /// </summary>
        public string RefundState { get; set; } = "";

        /// <summary>
        /// 拒绝退款原因
        /// </summary>
        public string NotRefundReason { get; set; } = "";


        /// <summary>
        /// 发票类型
        /// </summary>
        public string InvoiceType { get; set; } = "";

        /// <summary>
        /// 发票抬头
        /// </summary>
        public string InvoiceTitle { get; set; } = "";

        /// <summary>
        /// 收票人
        /// </summary>
        public string Taker { get; set; } = "";

        /// <summary>
        /// 收票电话
        /// </summary>
        public string TakerPhone { get; set; } = "";

        /// <summary>
        /// 单位名称
        /// </summary>
        public string UnitName { get; set; } = "";


        /// <summary>
        /// 单位税号
        /// </summary>
        public string UnitTax { get; set; } = "";

        /// <summary>
        /// 单位电话
        /// </summary>
        public string UnitPhone { get; set; } = "";

        /// <summary>
        /// 单位地址
        /// </summary>
        public string UnitAddress { get; set; } = "";

        /// <summary>
        /// 银行账号
        /// </summary>
        public string UnitAccount { get; set; } = "";

        /// <summary>
        /// 开票状态
        /// </summary>
        public string InvoiceState { get; set; } = "";


        /// <summary>
        /// 创建时间
        /// </summary>
        public string CreateTime { get; set; } = "";

        /// <summary>
        /// 商品名称
        /// </summary>
        public string GoodsName { get; set; } = "";

        /// <summary>
        /// 商品规格名称
        /// </summary>
        public string GoodsItemName { get; set; } = "";

        /// <summary>
        /// 商品单价
        /// </summary>
        public string GoodsPrice { get; set; } = "";

        /// <summary>
        /// 商品数量
        /// </summary>
        public string GoodsNum { get; set; } = "";

        /// <summary>
        /// 商品总价
        /// </summary>
        public string GoodsTotalPrice { get; set; } = "";

    }

    public class AdressDTO
    { 
        public string Address { set; get; }

        public string DetailedAddress { set; get; }
    }

    public class ChangeAddressInfoDTO { 
        
        public string Id { set; get; } 
        /// <summary>
        /// 收货人
        /// </summary>
        public string payeinfoName { set; get; }
        /// <summary>
        /// 收货地址
        /// </summary>
        public string payeinfoAddress { set; get; }
        /// <summary>
        /// 收货人电话
        /// </summary>
        public string payeinfoPhone { set; get; }

    }


    /// <summary>
    /// 统计数量
    /// </summary>
    public class orderStatistics
    {
        /// <summary>
        /// 成交订单量
        /// </summary>
       public int payCount { get; set; }

        /// <summary>
        /// 总成交量
        /// </summary>
       public int allpayCount { get; set; }
    }

    public class Statistics
    {
        public int State { get; set; }
        public int Count { get; set; }
        public string StateValue { get => Enum.GetName(typeof(States), State); }
    }
    public enum States
    {
        待支付 = 0,
        待收货 = 2,
        待评价 = 3

    }


    #region 后台获取订单

    public class Order_DtoTwo
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderCore { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public int? state { get; set; }

        /// <summary>
        /// 购买人姓名
        /// </summary>
        public string Shop_Name { get; set; }

        /// <summary>
        /// 购买人电话
        /// </summary>
        public string Shop_Phone { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? startTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? endTime { get; set; } = DateTime.Now;
    }

    public class Order_Dto
    {
        /// <summary>
        /// 订单Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 留言
        /// </summary>
        public string Note { get; set; }

        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName { get => _Decimal?.FirstOrDefault()?.ProductNames; }

        /// <summary>
        /// 数量
        /// </summary>
        public int? Num { get => _Decimal.Sum(p => p.Products_Num); }

        /// <summary>
        /// 购买人姓名
        /// </summary>
        public string NameUser { get; set; }

        /// <summary>
        /// 订单编号
        /// </summary>
        public string NumCode { get; set; }

        /// <summary>
        /// 发票信息
        /// </summary>
        public Invoice_Info Invoice { get; set; }

        /// <summary>
        /// 发票Id
        /// </summary>
        public string InvoiceId { get; set; }

        /// <summary>
        /// 购买人姓名
        /// </summary>
        public string PhoneUser { get; set; }

        /// <summary>
        /// 金额
        /// </summary>
        public decimal? AllPrice { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public int? OrderState { get; set; }
        /// <summary>
        /// 支付方式
        /// </summary>
        public int? PayState { get; set; }

        /// <summary>
        /// 订单时间
        /// </summary>
        public DateTime? CreatTime { get; set; }

        /// <summary>
        /// 发票信息
        /// </summary>
        public string InvoiceData { get; set; }

        /// <summary>
        /// 发票状态
        /// </summary>
        public int InvoiceState { get; set; }

        /// <summary>
        /// 收货人姓名
        /// </summary>
        public string payeinfoName { get; set; }

        /// <summary>
        /// 收货人地址
        /// </summary>
        public string payeinfoAddress { get; set; }

        /// <summary>
        /// 收货人电话
        /// </summary>
        public string payeinfoPhone { get; set; }

        /// <summary>
        ///应收金额
        /// </summary>
        public decimal YSMoney { get; set; } = 0;

        public BS_UserVailTicket _UTicketDTO { get; set; }

        /// <summary>
        /// 商品详情信息
        /// </summary>
        public IEnumerable<Order_Decimal> _Decimal { get; set; }

        public string LscCompany { get; set; }
        public string LscNum { get; set; }
    }

    #endregion 后台获取订单
    #region

    /// <summary>
    /// 订单信息
    /// </summary>
    public class OrderProducts
    {
        /// <summary>
        /// 订单Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderCode { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public DateTime? CreatTime { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public long? unixTime { get; set; }

        /// <summary>
        /// AddressId
        /// </summary>
        public string AddressId { get; set; }

        /// <summary>
        /// 总价格
        /// </summary>
        public decimal? TatolPrice { get; set; }

        /// <summary>
        /// 收款人姓名
        /// </summary>
        public String payeinfoName { get; set; }

        /// <summary>
        /// 收款人地址
        /// </summary>
        public String payeinfoAddress { get; set; }

        /// <summary>
        /// 收款人电话
        /// </summary>
        public String payeinfoPhone { get; set; }

        /// <summary>
        /// 发票信息
        /// </summary>
        public String InvoiceData { get; set; }

        /// <summary>
        /// 发票状态
        /// </summary>
        public int InvoiceState { get; set; }

        /// <summary>
        /// 已支付未支付
        /// </summary>
        public int? state { get; set; }

        /// <summary>
        /// 支付方式 1支付宝   2微信  3 在线支付
        /// </summary>
        public int? payState {get;set;}
        /// <summary>
        /// 支付方式 1支付宝   2微信  3 在线支付
        /// </summary>
        public string payStateType { get => Enum.GetName(typeof(payStateType), payState == null ? 0 : payState) ; }

        /// <summary>
        /// 产品
        /// </summary>
        public IEnumerable<OrderProduct> orderProducts { get; set; }

        /// <summary>
        /// 代金券
        /// </summary>
        public BS_UserVailTicket TicketDTO { get; set; }

        public bool IsPointOrder { get; set; } = false;

        public int? TotalPoint { get; set; } = 0;
        /// <summary>
        /// 是否已评论
        /// </summary>
        public bool IsDiscuss { get; set; } = false;

        /// <summary>
        ///应收金额
        /// </summary>
        public decimal YSMoney { get; set; } = 0;

        /// <summary>
        /// 物流公司
        /// </summary>
        public String LscCompany { get; set; }

        /// <summary>
        /// 物流号码
        /// </summary>
        public String LscNumber { get; set; }


        /// <summary>
        /// 退款失败原因
        /// </summary>
        public string NotRefundReasion { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public String Note { get; set; }
    }

    /// <summary>
    /// 发票类型Rise
    /// </summary>
    public enum Rise
    {
        发票 = -1,
        个人 = 0,
        单位 = 1,
    }
    /// <summary>
    /// 发票类型Rise
    /// </summary>
    public enum payStateType
    {
        未支付=0,
        支付宝 =1,
        微信小程序 = 2,
        微信公众号 = 3
    }

    /// <summary>
    /// 发票类型Rise
    /// </summary>
    public enum OrderStateType
    {
        待支付 = 0,
        代发货 = 1,
        待收货 = 2,
        待评价 = 3,
        已完成 = 4,
        已退货 = 5
    }

    /// <summary>
    /// 发票信息
    /// </summary>
    public enum Invoic_Type
    {
        暂无 = -1,
        电子发票 = 0,
        纸质发票 = 1,
    }

    public class OrderProduct
    {
        /// <summary>
        /// id
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 商品Id
        /// </summary>
       public string productId { get; set; }

        /// <summary>
        /// 商品Id
        /// </summary>
        public string productItemId { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 商品规格名称
        /// </summary>
        public string itemName { get; set; }

        /// <summary>
        /// 商品价格
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 商品数量
        /// </summary>
        public int Num { get; set; }

        /// <summary>
        /// 商品图片
        /// </summary>
        public string Image { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public string DATA0 { get; set; }
    }

    #endregion

    /// <summary>
    ///返回数据
    /// </summary>
    public class OrderNum
    {
        public string code { get; set; }
        public string msg { get; set; }

        public decimal? price { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long DataSpan { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Data { get; set; }

        public string Order_Id { get; set; }
    }

    public class ProductDto
    {
        /// <summary>
        /// 商品Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 商品规格Id
        /// </summary>
        public string itemId { get; set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }
    }

    public class Order_MasterDto
    {
        public String Id { get; set; }

        /// <summary>
        /// CreatorId
        /// </summary>
        public String CreatorId { get; set; }

        /// <summary>
        /// CreateTime
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// Deleted
        /// </summary>
        public Boolean? Deleted { get; set; }

        /// <summary>
        /// 订单编号
        /// </summary>
        public String OrderCode { get; set; }

        /// <summary>
        /// 收货人信息
        /// </summary>
        public String Address_Id { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public OrderStateS OrderState { get; set; }

        /// <summary>
        /// 支付状态
        /// </summary>
        public PayStates PayState { get; set; }

        /// <summary>
        /// 总价格
        /// </summary>
        public Decimal? TotalPrice { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }
    }

    #region 添加订单

    /// <summary>
    /// 添加订单接受的参数订单Dto
    /// </summary>
    public class OrderDto
    {
        /// <summary>
        /// 商品Id
        /// </summary>

        public List<Product> _products { get; set; }

        /// <summary>
        /// 地址Id
        /// </summary>
        [Required(ErrorMessage ="{0}地址是必须的")]
        public string AddressId { get; set; }

        /// <summary>
        /// 发票Id
        /// </summary>
        public string Invoice_Id { get; set; }

        /// <summary>
        /// 订单标识 0：购物车生成，1：直接生成
        /// </summary>
        public int OrderFlag { get; set; }

        /// <summary>
        /// 购物车Id
        /// </summary>
        public List<string> Cart { get; set; }
    }

    /// <summary>
    /// 添加订单接受的参数订单Dto
    /// </summary>
    public class SetOrderDto
    {
        /// <summary>
        /// 商品Id
        /// </summary>
        public List<BuyGoodDTO> GoodItems { get; set; }

        /// <summary>
        /// 地址Id
        /// </summary>
        [Required(ErrorMessage = "{0}地址是必须的")]
        public string AddressId { get; set; }

        /// <summary>
        /// 发票Id
        /// </summary>
        public string Invoice_Id { get; set; }

        /// <summary>
        /// 订单标识 0：购物车生成，1：直接生成
        /// </summary>
        public int OrderFlag { get; set; }

        /// <summary>
        /// 用户留言
        /// </summary>
        public string Note { get; set; }

        /// <summary>
        /// 购物车Id
        /// </summary>
        public List<string> CartIds { get; set; }

        /// <summary>
        /// 代金券Id
        /// </summary>
        public string VailTicketId { get; set; }
    }

    public class BuyGoodDTO {

        /// <summary>
        /// 产品ID
        /// </summary>
        [Required(ErrorMessage = "{0}产品ID是必须的")]
        public string GoodItemId { get; set; }

        /// <summary>
        /// 产品ID
        /// </summary>
        [Required(ErrorMessage = "{0}产品数量是必须的")]
        public int GoodNum { get; set; }
    }

    public class Product
    {
        /// <summary>
        /// 产品ID
        /// </summary>
        [Required(ErrorMessage = "{0}产品ID是必须的")]
        public string GoodId { get; set; }

        /// <summary>
        /// 产品规格ID
        /// </summary>
        [Required(ErrorMessage ="{0}产品规格是必须的")]
        public string GoodItemId { get; set; }

        /// <summary>
        /// 产品名称
        /// </summary>
        public String ProductNames { get; set; }

        /// <summary>
        /// 产品规格
        /// </summary>
        public String ProductitemNames { get; set; }

        /// <summary>
        /// 产品图片
        /// </summary>

        public String ProductImage { get; set; }

        /// <summary>
        /// 产品价格
        /// </summary>
        [Required(ErrorMessage = "{0}产品价格是必须的")]
        public decimal ProductPrice { get; set; }


        /// <summary>
        /// 数量
        /// </summary>
        [Required(ErrorMessage = "{0}数量是必须的")]
        public int Num { get; set; }
    }

    /// <summary>
    /// 添加订单接受的参数订单Dto
    /// </summary>
    public class PointOrderDto
    {
        /// <summary>
        /// 商品列表
        /// </summary>

        public List<PointProduct> _products { get; set; }

        /// <summary>
        /// 地址Id
        /// </summary>
        [Required(ErrorMessage = "{0}地址是必须的")]
        public string AddressId { get; set; }

        /// <summary>
        /// 订单标识 0：购物车生成，1：直接生成
        /// </summary>
        public int OrderFlag { get; set; }

        /// <summary>
        /// 购物车Id
        /// </summary>
        public List<string> Cart { get; set; }
    }


    public class PointProduct
    {
        /// <summary>
        /// 产品ID
        /// </summary>
        [Required(ErrorMessage = "{0}产品ID是必须的")]
        public string GoodId { get; set; }

        /// <summary>
        /// 产品规格ID
        /// </summary>
        [Required(ErrorMessage = "{0}产品规格是必须的")]
        public string GoodItemId { get; set; }

        /// <summary>
        /// 产品名称
        /// </summary>
        public String ProductNames { get; set; }

        /// <summary>
        /// 产品规格
        /// </summary>
        public String ProductitemNames { get; set; }

        /// <summary>
        /// 产品图片
        /// </summary>

        public String ProductImage { get; set; }

        /// <summary>
        /// 产品价格
        /// </summary>
        public int PointPrice { get; set; } = 0;


        /// <summary>
        /// 数量
        /// </summary>
        [Required(ErrorMessage = "{0}数量是必须的")]
        public int Num { get; set; }
    }

    #endregion

    #region  取消订单

    /// <summary>
    /// 商品Id
    /// </summary>
    public class CannclOrder
    {
        public string Id { get; set; }
    }

    #endregion

    /// <summary>
    /// 订单状态
    /// </summary>
    public enum OrderStateS
    {
        待付款 = 0,
        待发货 = 1,
        已发货 = 2,
        已收货 = 3,
        已完成 = 4,
        已取消 = 5,
        已确认 = 6,
        申请退款中 = 7,
        已退款 = 8,
        拒绝退款 = 9
    }

    /// <summary>
    /// 支付状态
    /// </summary>
    public enum PayStates
    {
        待付款 = 0,
        已付款 = 1,
    }


    public class ToSendOrderDTO {

        /// <summary>
        /// Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 物流公司
        /// </summary>
        public string Conmpany { get; set; }

        /// <summary>
        /// 物流状态
        /// </summary>
        public string Number { get; set; }
    }

}